package com.example.administrator.myokhttpmvp.main.login;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.myokhttpmvp.R;
import com.example.administrator.myokhttpmvp.main.login.presenter.ILoginPresenter;
import com.example.administrator.myokhttpmvp.main.login.presenter.LoginPresenterCompl;
import com.example.administrator.myokhttpmvp.main.login.view.ILoginView;
import com.example.administrator.myokhttpmvp.net.NetCallBackImp;
import com.example.administrator.myokhttpmvp.net.ServerApi;
import com.example.administrator.myokhttpmvp.net.UserNet;
import com.example.administrator.myokhttpmvp.utils.PrefManager;
import com.example.administrator.myokhttpmvp.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by wq on 2018/10/10.
 */
public class loginActivity extends Activity implements ILoginView,View.OnClickListener{
    private EditText mMsmCode, vPhoneTv;
    private TextView mRegisterBt, vSend;

    private ILoginPresenter loginPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        initEvent();
        initData();
    }

    protected void initView() {
        mMsmCode = (EditText) findViewById(R.id.mMsmCode);
        vPhoneTv = (EditText) findViewById(R.id.vPhoneTv);
        mRegisterBt = (TextView) findViewById(R.id.mRegisterBt);
        vSend = (TextView) findViewById(R.id.tvSend);
        //init
        loginPresenter = new LoginPresenterCompl(this);
        loginPresenter.setProgressBarVisiblity(View.INVISIBLE);

    }

    protected void initEvent() {
        vSend.setOnClickListener(this);
        mRegisterBt.setOnClickListener(this);


    }

    protected void initData() {
    }


    private void mobileCode() {
            //执行网络请求接口
            try {
                UserNet.Instance().mobileCode(vPhoneTv.getText().toString(), new NetCallBackImp() {
                    @Override
                    public void pareOk(JSONObject data) {
                        super.pareOk(data);
                        ToastUtil.show(loginActivity.this, "发送成功！");
                    }

                    @Override
                    public void paresOther(String code, String message) {
                        super.paresOther(code, message);
                    }

                    @Override
                    public void requestFinish() {
                        super.requestFinish();
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mRegisterBt:
                mRegisterBt.setEnabled(false);
                loginPresenter.doLogin(vPhoneTv.getText().toString(), mMsmCode.getText().toString());
                break;
            case R.id.tvSend:
                mobileCode();
                break;
        }
    }

    @Override
    public void onClearText() {
        mMsmCode.setText("");
        vPhoneTv.setText("");
    }

//    登录成功的结果
    @Override
    public void onLoginResultSucess(Boolean result, int code,String response ) {
//        loginPresenter.setProgressBarVisiblity(View.INVISIBLE);
        mRegisterBt.setEnabled(true);

        if (result){

            Toast.makeText(this,"Login Success",Toast.LENGTH_SHORT).show();
        }
        else
            Toast.makeText(this,"Login Fail, code = " + code,Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onLoginResultOther(Boolean result, int code, String response) {
        mRegisterBt.setEnabled(true);

    }

    @Override
    public void onSetProgressBarVisibility(int visibility) {
//        progressBar.setVisibility(visibility);
    }
}
